package com.ruoyi.datastatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.PageUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.datastatistics.domain.TerminalsDayfreezeDb;
import com.ruoyi.datastatistics.domain.TerminalsFreezeXb;
import com.ruoyi.datastatistics.domain.TerminalsMonfreezeDb;
import com.ruoyi.datastatistics.mapper.TerminalsDayfreezeDbMapper;
import com.ruoyi.datastatistics.model.TerminalsDayRangeFreezeDbResultModel;
import com.ruoyi.datastatistics.service.ITerminalsDayfreezeDbService;
import com.ruoyi.datastatistics.vo.DbHourUsageVO;
import com.ruoyi.datastatistics.vo.HourUsageData;
import com.ruoyi.datastatistics.vo.MiniFlowComparisonVO;
import com.ruoyi.datastatistics.vo.PressureWatchVO;
import com.ruoyi.system.domain.SysSheetUsers;
import com.ruoyi.system.mapper.SysSheetUsersMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 大表日用量数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@Service
public class TerminalsDayfreezeDbServiceImpl implements ITerminalsDayfreezeDbService {
    @Autowired
    private TerminalsDayfreezeDbMapper terminalsDayfreezeDbMapper;
    @Autowired
    private SysSheetUsersMapper sysSheetUsersMapper;

    /**
     * 查询大表日用量数据
     *
     * @param Id 大表日用量数据主键
     * @return 大表日用量数据
     */
    @Override
    public TerminalsDayfreezeDb selectTerminalsDayfreezeDbById(Long Id) {
        return terminalsDayfreezeDbMapper.selectTerminalsDayfreezeDbById(Id);
    }

    /**
     * 查询大表日用量数据列表
     *
     * @param terminalsDayfreezeDb 大表日用量数据
     * @return 大表日用量数据
     */
    @Override
    public List<TerminalsDayfreezeDb> selectTerminalsDayfreezeDbList(TerminalsDayfreezeDb terminalsDayfreezeDb) {
        return terminalsDayfreezeDbMapper.selectTerminalsDayfreezeDbList(terminalsDayfreezeDb);
    }


    @Override
    public List<TerminalsDayfreezeDb> getTerminalsDayfreezeDbList(Map<String, Object> paramMap) {
        return terminalsDayfreezeDbMapper.getTerminalsDayfreezeDbList(paramMap);
    }

    @Override
    public List<TerminalsDayfreezeDb> getTerminalsDayfreezeDbLists(String TerminalID, String startTime, String endTime) {
        return terminalsDayfreezeDbMapper.getTerminalsDayfreezeDbLists(TerminalID, startTime, endTime);
    }

    /**
     * 新增大表日用量数据
     *
     * @param terminalsDayfreezeDb 大表日用量数据
     * @return 结果
     */
    @Override
    public int insertTerminalsDayfreezeDb(TerminalsDayfreezeDb terminalsDayfreezeDb) {
        return terminalsDayfreezeDbMapper.insertTerminalsDayfreezeDb(terminalsDayfreezeDb);
    }

    /**
     * 修改大表日用量数据
     *
     * @param terminalsDayfreezeDb 大表日用量数据
     * @return 结果
     */
    @Override
    public int updateTerminalsDayfreezeDb(TerminalsDayfreezeDb terminalsDayfreezeDb) {
        return terminalsDayfreezeDbMapper.updateTerminalsDayfreezeDb(terminalsDayfreezeDb);
    }

    /**
     * 批量删除大表日用量数据
     *
     * @param Ids 需要删除的大表日用量数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsDayfreezeDbByIds(Long[] Ids) {
        return terminalsDayfreezeDbMapper.deleteTerminalsDayfreezeDbByIds(Ids);
    }

    /**
     * 删除大表日用量数据信息
     *
     * @param Id 大表日用量数据主键
     * @return 结果
     */
    @Override
    public int deleteTerminalsDayfreezeDbById(Long Id) {
        return terminalsDayfreezeDbMapper.deleteTerminalsDayfreezeDbById(Id);
    }

    @Override
    public List<TerminalsDayRangeFreezeDbResultModel> getTerminalsDayRangeFreezeDbList(Map<String, Object> paramMap) {
        return terminalsDayfreezeDbMapper.getTerminalsDayRangeFreezeDbList(paramMap);
    }

    @Override
    public List<TerminalsDayfreezeDb> listThirtyDaysFlow(TerminalsDayfreezeDb terminalsDayfreezeDb) {
        return terminalsDayfreezeDbMapper.selectList(new QueryWrapper<TerminalsDayfreezeDb>().eq("terminal_id", terminalsDayfreezeDb.getTerminalId())
                .ge("get_time_n", terminalsDayfreezeDb.getStartTime()).le("get_time_n", terminalsDayfreezeDb.getEndTime()).orderByDesc("get_time_n").last(" limit 30"));
    }

    @Override
    public List<TerminalsDayfreezeDb> reportListByDay(TerminalsDayfreezeDb terminalsDayfreezeDb) {
        Long sysUserId = (Long) terminalsDayfreezeDb.getParams().get("sysUserId");
        Boolean export = (Boolean) terminalsDayfreezeDb.getParams().get("export");
        if (sysUserId != null) {
            List<SysSheetUsers> sheetUsersList = sysSheetUsersMapper.selectList(new LambdaQueryWrapper<SysSheetUsers>().eq(SysSheetUsers::getSysUserid, sysUserId.intValue()));
            Set<Integer> sheetIds = sheetUsersList.stream().map(SysSheetUsers::getSheetid).collect(Collectors.toSet());
            terminalsDayfreezeDb.setSheetId(StringUtils.join(sheetIds, ","));
        }
        if (export == null || !export) {
            PageUtils.startPage();
        }
        return terminalsDayfreezeDbMapper.reportListByDay(terminalsDayfreezeDb);
    }

    @Override
    public List<DbHourUsageVO> hourUsages(DbHourUsageVO usageVO) {
        List<DbHourUsageVO> dbHourUsageVOList = terminalsDayfreezeDbMapper.selectHourUsages(usageVO);
        for (DbHourUsageVO vo : dbHourUsageVOList) {
            List<HourUsageData> data = vo.getUsage();
            if (CollectionUtils.isNotEmpty(data)) {
                for (HourUsageData usage : data) {
                    ReflectUtils.setFieldValue(vo, "h" + usage.getHour(), usage.getUsage());
                }
            }
        }
        return dbHourUsageVOList;
    }

    @Override
    public Long countHourUsages(DbHourUsageVO usageVO) {
        return terminalsDayfreezeDbMapper.countHourUsages(usageVO);
    }

    @Override
    public List<MiniFlowComparisonVO> getMiniFlowComparisonList(MiniFlowComparisonVO miniFlowComparisonVO) {
        return terminalsDayfreezeDbMapper.selectMiniFlowComparisonList(miniFlowComparisonVO);
    }

    @Override
    public List<PressureWatchVO> getPressureWatchList(PressureWatchVO watchVO) {
        if (watchVO.getSysUseId() != null){
            List<SysSheetUsers> sheetUsersList = sysSheetUsersMapper.selectList(new LambdaQueryWrapper<SysSheetUsers>().eq(SysSheetUsers::getSysUserid, watchVO.getSysUseId().intValue()));
            Set<Integer> sheetIds = sheetUsersList.stream().map(SysSheetUsers::getSheetid).collect(Collectors.toSet());
            watchVO.setSheetsId(StringUtils.join(sheetIds,","));
        }
        return terminalsDayfreezeDbMapper.selectPressureWatchList(watchVO);
    }

}
